package com.pig4cloud.pigx.common.customize.redis.config;

import lombok.AllArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 *
 * @since 2022/9/19 19:53
 */
@Configuration
@AllArgsConstructor
public class redissonConfig {

	private final RedisConfig redisConfig;

	/**
	 * 对 Redisson 的使用都是通过 RedissonClient 对象
	 *
	 * @return {@link RedissonClient}
	 * @since 2022/9/19 20:04
	 */
	@Bean(destroyMethod = "shutdown") // 服务停止后调用 shutdown 方法。
	public RedissonClient redisson() {
		String addressFormat = "redis://%s:%s";

		// 1.创建配置
		Config config = new Config();
		// 集群模式
		// config.useClusterServers().addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
		// 2.根据 Config 创建出 RedissonClient 示例。
		String address = String.format(addressFormat, redisConfig.getHost(), redisConfig.getPort());
		config.useSingleServer()
				.setAddress(address)
				.setPassword(redisConfig.getPassword());

		return Redisson.create(config);
	}
}
